package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.EquipmentMaintenance;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 设备维护记录表(EquipmentMaintenance)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:25
 */
public interface EquipmentMaintenanceDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from equipment_maintenance where id=#{id}")
  EquipmentMaintenance queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param equipmentMaintenance 查询条件
   * @param pageSize             分页对象
   * @param offset               偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from equipment_maintenance where 1=1" +
          " and (id=#{equipmentMaintenance.id} or (isnull(#{equipmentMaintenance.id}) or #{equipmentMaintenance.id}=0))" +
          " and (equipment_id=#{equipmentMaintenance.equipmentId} or (isnull(#{equipmentMaintenance.equipmentId}) or #{equipmentMaintenance.equipmentId}=0))" +
          " and (maintenance_date=#{equipmentMaintenance.maintenanceDate} or (isnull(#{equipmentMaintenance.maintenanceDate}) or #{equipmentMaintenance.maintenanceDate}=''))" +
          " and (details=#{equipmentMaintenance.details} or (isnull(#{equipmentMaintenance.details}) or #{equipmentMaintenance.details}=''))" +
          " and (responsible_person=#{equipmentMaintenance.responsiblePerson} or (isnull(#{equipmentMaintenance.responsiblePerson}) or #{equipmentMaintenance.responsiblePerson}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<EquipmentMaintenance> queryAllByLimit(EquipmentMaintenance equipmentMaintenance, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from equipment_maintenance")
  long count();

  /**
   * 新增数据
   *
   * @param equipmentMaintenance 实例对象
   * @return 影响行数
   */
  @Insert("insert into equipment_maintenance (id,equipment_id,maintenance_date,details,responsible_person) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{equipmentId}),#{equipmentId},null)," +
          "if(!isnull(#{maintenanceDate}),#{maintenanceDate},null)," +
          "if(!isnull(#{details}),#{details},null)," +
          "if(!isnull(#{responsiblePerson}),#{responsiblePerson},null)" +
          ")")
  int insert(EquipmentMaintenance equipmentMaintenance);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param equipmentMaintenance 实例对象
   * @return 影响行数
   */
  @Update("update equipment_maintenance set " +
          "equipment_id=if(isnull(#{equipmentId}),equipment_id,#{equipmentId})," +
          "maintenance_date=if(isnull(#{maintenanceDate}),maintenance_date,#{maintenanceDate})," +
          "details=if(isnull(#{details}),details,#{details})," +
          "responsible_person=if(isnull(#{responsiblePerson}),responsible_person,#{responsiblePerson})" +
          " where id=#{id}")
  int update(EquipmentMaintenance equipmentMaintenance);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from equipment_maintenance where id=#{id}")
  int deleteById(Integer id);
}

